home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15263 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  43 lines

  1. Path: qualcomm.com!not-for-mail
  2. From: drew@qualcomm.com (Drew Eckhardt)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Implementation of 'strncmpi' function needed
  5. Date: 18 Apr 1996 02:09:26 -0600
  6. Organization: QUALCOMM, Incorporated; Boulder, CO, USA
  7. Message-ID: <4l4tbm$4ko@qualcomm.com>
  8. References: <4l4q21$qtv@nadine.teleport.com>
  9. NNTP-Posting-Host: littlebear.qualcomm.com
  10.  
  11. In article <4l4q21$qtv@nadine.teleport.com>,
  12. GHouck  <hksys@teleport.com> wrote:
  13. >Howdy,
  14. >
  15. >I have to use a quasi-C implementation that does not include
  16. >a 'strncmpi' function, and therefore have created the following
  17. >function to accomplish the task.  
  18.  
  19. Some environments have such a function, but use the BSD name (strncasecmp). 
  20.  
  21. < About thirty lines of pain deleted, involving dynamic memory allocation,
  22.   multiple traversals of the strings, and other owies >
  23.  
  24. What's wrong with something simple like this instead?
  25.  
  26.    #include <ctype.h>
  27.    #include <assert.h>
  28.    int
  29.    strncmpi (const char *a, const char *b, int len) {
  30.       for (;len && toupper(*a) == toupper(*b); --len, ++a, ++b);
  31.       return (toupper(*a) - toupper(*b));
  32.    }
  33.  
  34. A toupper() implementation for non-hosted ANSI environments, accomodating 
  35. compilers that don't distinguish between const char * and char const * 
  36. (SCO and MicroSoft used to do this), compiler-crutches (such as temporaries 
  37. with a register storage class request), and various other micro-optomizations 
  38. are left as exercises for the reader.
  39. -- 
  40. <a href="http://www.poohsticks.org/drew/">Home Page</a>
  41. Four boxes : soap, ballot, jury, ammo.  | Work: drew@Qualcomm.COM       
  42. Use in that order.                      | Play: drew@PoohSticks.ORG    
  43.